package com.demonzym.activity.data;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ItemInfoData {
	public String title, description, ownerName, ownerNickname, tags, outerPlayerUrl, alias,
	location, picUrl, pubDate, mediaType, itemUrl, bigPicUrl, itemCode, addPlaylistTime, ownerPic,
	ownerURL, html5Url;
	public int playTimes, totalTime, channelId, itemId, ownerId, commentCount, favorall, definition;
	public boolean downEnable, secret;
	public String[] picChoiceUrl;
	
	//0:256p,1:360p,2:480p,3:720p
	private static String[] mDefinitionSrt = new String[]{
			"256P",
			"360P",
			"480P",
			"720P",
	};
	public static String getDefinition(int i){
		if(i >-1 && i < 4){
			return mDefinitionSrt[i];
		} else {
			return "";
		}
	}
	
	public static ItemInfoData[] parseItem(String str){
		ItemInfoData[] data = null;
		try {
			String multiResult = new JSONObject(str).getString("multiResult");
			JSONObject obj = new JSONObject(multiResult);
			JSONArray jarray = obj.getJSONArray("results");
			data = new ItemInfoData[jarray.length()];
			for(int i = 0; i < data.length; i++){
				JSONObject jobj = jarray.getJSONObject(i);
				data[i] = new ItemInfoData();
				data[i].location = jobj.getString("location");
				data[i].description = jobj.getString("description");
				data[i].tags = jobj.getString("tags");
				data[i].picUrl = jobj.getString("picUrl");
				data[i].title = jobj.getString("title");
				data[i].playTimes = jobj.getInt("playTimes");
				data[i].pubDate = jobj.getString("pubDate");
				data[i].mediaType = jobj.getString("mediaType");
				data[i].totalTime = jobj.getInt("totalTime");
				data[i].downEnable = jobj.getBoolean("downEnable");
				data[i].channelId = jobj.getInt("channelId");
				data[i].alias = jobj.getString("alias");
				data[i].itemId = jobj.getInt("itemId");
				data[i].ownerId = jobj.getInt("ownerId");
				data[i].ownerNickname = jobj.getString("ownerNickname");
				data[i].itemUrl = jobj.getString("itemUrl");
				data[i].favorall = jobj.getInt("favorall");
				data[i].bigPicUrl = jobj.getString("bigPicUrl");
				data[i].definition = jobj.getInt("definition");
				data[i].ownerName = jobj.getString("ownerName");
				data[i].outerPlayerUrl = jobj.getString("outerPlayerUrl");
				data[i].itemCode = jobj.getString("itemCode");
				JSONArray array = ((JSONArray) jobj.get("picChoiceUrl"));
				data[i].picChoiceUrl = new String[array.length()];
				for(int j = 0; j < array.length(); j++){
					data[i].picChoiceUrl[j] = array.getString(j);
				}
				data[i].secret = jobj.getBoolean("secret");
				data[i].addPlaylistTime = jobj.getString("addPlaylistTime");
				data[i].ownerPic = jobj.getString("ownerPic");
				data[i].ownerURL = jobj.getString("ownerURL");
				data[i].html5Url = jobj.getString("html5Url");
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return data;
	}
}
